﻿/// <reference path="../../Scripts/jquery-1.6.1.js" />
(function ($) {
    var ddlControl = {
        btnObj: null,
        txtObj: null,
        listObj: null,
        isExpanded: false,
        onTriggerClick: function (e) {
            var self = e.data;
            if (self) {
                var ddl = self.ddlControl;
                if (ddl.isExpanded) {
                    ddl.collapse();
                } else {
                    ddl.expand();
                }
            }
        },
        mimicBlur: function (e) {
            var ddl = e.data;
            var tag = event.target || event.srcElement;
            if (ddl && ddl.listObj.length > 0 && ddl.btnObj.length > 0) {
                if (!ddl.listObj[0].contains(tag) && !ddl.btnObj[0].contains(tag)) {
                    ddl.collapse();
                }
            }
        },
        collapse: function () {
            var ddl = this;
            ddl.listObj.hide();
            ddl.btnObj.removeClass("d-form-trigger-done").addClass("d-form-trigger");
            ddl.isExpanded = false;
            $(ddl.listObj.context.body).unbind("mousedown", this.mimicBlur);
        },
        expand: function () {
            var ddl = this;
            ddl.listObj.show();
            ddl.btnObj.removeClass("d-form-trigger").addClass("d-form-trigger-done");
            ddl.isExpanded = true;
            ddl.alignList();
            $(ddl.listObj.context.body).bind("mousedown", ddl, ddl.mimicBlur);
        },
        alignList: function () {
            var ddl = this;
            if (ddl.isExpanded) {
                var xy = ddl.getListXY();
                ddl.listObj.offset(xy);
            }
        },
        getListXY: function () {
            var el = this.txtObj;
            var op = el.offset()
            return { "left": op.left, "top": op.top + el.outerHeight() - 1 };
        },
        itemSelect: function (e) {
            var ddl = e.data;
            if (ddl) {
                var tag = event.target || event.srcElement;
                ddl.txtObj.val($(tag).text());
                $("li.d-boundlist-item-selected", ddl.listObj).removeClass("d-boundlist-item-selected").addClass("d-boundlist-item");
                $(tag).removeClass("d-boundlist-item").addClass("d-boundlist-item-selected");
                ddl.collapse();
            }
        }
    };
    jQuery.prototype.extend({ "ddlControl": ddlControl });
    $.fn.ddlInit = function () {
        var self = this;
        if (self.length > 0) {
            var ddl = self.ddlControl;
            ddl.btnObj = $("#" + self[0].id + "_btn");
            ddl.txtObj = $("#" + self[0].id);
            ddl.listObj = $("#" + self[0].id + "_list");
            ddl.btnObj.bind("click", self, ddl.onTriggerClick);
            $("li.d-boundlist-item,li.d-boundlist-item-selected", ddl.listObj).bind("click", ddl, ddl.itemSelect);
        }
    };
})(jQuery);
